package com.charging.controller.user;

import com.charging.entity.Announcement;
import com.charging.entity.ChargingPile;
import com.charging.entity.User;
import com.charging.service.AnnouncementService;
import com.charging.service.ChargingPileService;
import com.charging.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;
    @Autowired
    private AnnouncementService announcementService;
    @Autowired
    private ChargingPileService chargingPileService;


    @GetMapping("/login")
    public String showLoginForm() {
        return "user/login";
    }

    // 处理登录请求
    @PostMapping("/login")
    public ModelAndView login(
            String username,
            String password
    )
    {
        ModelAndView mv = new ModelAndView();

        try {
            // 公共登录逻辑
            User user = userService.login(username, password);

            // 根据角色处理
            switch (user.getRole()) {
                case 0:
                    mv.setViewName("/user/home"); // 普通用户主页
                    break;
                case 1:
                    mv.setViewName("/admin/dashboard"); // 管理员仪表盘
                    break;
                case 2:
                    mv.setViewName("/tester/dashboard"); // 测试员工作区
                    break;
            }

        } catch (RuntimeException e) {
            mv.addObject("error", e.getMessage());
            mv.setViewName("/user/login");
            System.out.println("登录失败");
        }

        return mv;
    }

    @GetMapping("/register")
    public String showRegisterForm() {
        return "user/register";
    }

    @PostMapping("/register")
    public String register(User user, RedirectAttributes redirectAttributes) {
        try {
            userService.register(user);
            redirectAttributes.addFlashAttribute("success", "注册成功，请登录");
            return "/user/login";
        } catch (RuntimeException e) {
            redirectAttributes.addFlashAttribute("error", e.getMessage());
            System.out.println("注册失败");
            return "/user/register";
        }
    }

    // 用户主页
    @GetMapping("/home")
    public String userDashboard( Model model) {

        // 获取所有有效公告
        List<Announcement> announcements = announcementService.getAllValidAnnouncements();
        model.addAttribute("announcementList", announcements);

        List<ChargingPile> chargingPiles = chargingPileService.findAll();
        model.addAttribute("chargingPileList",chargingPiles);
        return "user/home";
    }

    // 退出登录
    @GetMapping("/logout")
    public String logout(HttpSession session) {
        session.removeAttribute("currentUser");
        return "redirect:/user/login";
    }

    @PostMapping("/update-location")
    public ResponseEntity<?> updateLocation(
            @RequestParam Long userId,
            @RequestParam Double lat,
            @RequestParam Double lng) {
        userService.updateUserLocation(userId, lat, lng);
        return ResponseEntity.ok().build();
    }

}
